package com.weakteam.orderonline.dataresource.service;

import com.weakteam.orderonline.dataresource.dao.GoodsDAO;
import com.weakteam.orderonline.dataresource.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Nail
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDAO goodsDAO;

    @Transactional
    @Override
    public List<Goods> getAll() {
        return goodsDAO.getAll();
    }

	@Transactional
    @Override
    public List<Goods> getAllEnabled() {
        return goodsDAO.getAllEnabled();
    }

    @Transactional
    @Override
    public List<Goods> getAllByMenuId(Integer menuId) {
        return goodsDAO.getAllByMenuId(menuId);
    }

    @Transactional
    @Override
    public List<Goods> getAllEnabledByMenuId(Integer menuId) {
        return goodsDAO.getAllEnabledByMenuId(menuId);
    }

    @Transactional
    @Override
    public Goods getById(Integer id) {
        return goodsDAO.getById(id);
    }

    @Transactional
    @Override
    public void save(Goods goods) {
        if (goods.getOrder() == null) {
            Integer maxOrder = this.goodsDAO.getMaxOrder(goods.getMenuId());
            goods.setOrder(maxOrder + 1);
        }

        if (goods.getName() == null) {
            goods.setName("new goods");
        }

        if (goods.getPrice() == null) {
            goods.setPrice(0.0);
        }

        if (goods.getEnabled() == null) {
            goods.setEnabled(false);
        }

        goodsDAO.save(goods);
    }

    @Transactional
    @Override
    public boolean delete(Integer id) {
        return goodsDAO.delete(id);
    }
}
